home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / System / ScreenDaemon 1.2 / ScreenDaemon appe / SDinit.c < prev    next >
C/C++ Source or Header  |  1996-03-16  |  13KB  |  469 lines

  1. /*********************************************************************
  2.  * ScreenDaemon INIT
  3.  *********************************************************************/
  4.  
  5. #include <Displays.h>
  6. #include "gamma.h"
  7. #include "SD.h"
  8. #include "SDinit.h"
  9.  
  10.  
  11.  
  12. #define Bail() { SysBeep(5); SysBeep(5); return false; }
  13.  
  14. void main()
  15. {
  16.     Ptr                    patchPtr;
  17.     PatchGlobalsPtr        pgPtr;
  18.     long                codeSize, offset, result, oldA5;
  19.     PrefStructureHandle    prefHandle;
  20.     Handle                procH;
  21.     Boolean                usingPrefsFile;
  22.     short                theResFile;
  23.     fQDGlobals            fqd;
  24.     GrafPort            gp;
  25.     GDHandle            theScreen;
  26.     Rect                theRect;
  27.  
  28.     /* Are we likely to be able to run? */
  29.     if (Gestalt('sysv', &result) != noErr || result < 0x700) {
  30.         DisplayIcon(kErrorIcon);
  31.         return;        /* We're pre-System 7, so our 'appe' won't likely load. Give up now. */
  32.     }
  33.  
  34.     if (!IsGammaAvailable()) {
  35.         DisplayIcon(kErrorIcon);
  36.         return;        /* we can't do a gamma fade, so we can't run */
  37.     }
  38.  
  39.     /* try and get some memory in the system heap for code and globals */
  40.     codeSize = (long) EndPatchCode - (long) StartPatchCode;
  41.     patchPtr = NewPtrSys(codeSize + sizeof(PatchGlobals));
  42.     if (!patchPtr) {
  43.         DisplayIcon(kErrorIcon);
  44.         return;        /* out of memory -- abort patching */
  45.     }
  46.     
  47.     /* Set up pgPtr */
  48.     pgPtr = (PatchGlobalsPtr) patchPtr;
  49.     
  50.     /* Reset pgPtr->pgVersion for use as a watch variable */
  51.     pgPtr->pgVersion = 0;
  52.     
  53.     /* Open up the preferences file */
  54.     theResFile = CurResFile();
  55.     usingPrefsFile = OpenPrefsFile();
  56.  
  57.     /* if usingPrefsFile == false then we're opening our internal prefs */
  58.     prefHandle = (PrefStructureHandle) GetIndResource('PREF', 1);
  59.     if (ResError() == noErr) {
  60.         pgPtr->pgVersion = (**prefHandle).version;
  61.         pgPtr->pgMustSleep = false;
  62.         pgPtr->pgSleepRect = (**prefHandle).sleepNowCorner;
  63.         pgPtr->pgWakeRect = (**prefHandle).sleepNeverCorner;
  64.         pgPtr->pgIdleTicks = ((long) (**prefHandle).idleTime) * 3600;
  65.         pgPtr->pgMinBrite = (**prefHandle).refreshSeconds;
  66.         pgPtr->pgMustSave = (**prefHandle).saverOn;
  67.         pgPtr->pgForceUpdates = (**prefHandle).forceUpdates;
  68.         ReleaseResource((Handle) prefHandle);
  69.         if (usingPrefsFile) {
  70.             CloseResFile(CurResFile());
  71.             UseResFile(theResFile);
  72.         }
  73.     }
  74.  
  75.     /* Double check to see if stuff *really* loaded */
  76.     if (pgPtr->pgVersion == 0) {
  77.         DisposPtr(patchPtr);
  78.         DisplayIcon(kErrorIcon);
  79.         return;        /* can't read in preferences - abort patching */
  80.     }
  81.  
  82.     /* initialize the patch globals at the beginning of the block */
  83.     pgPtr->pgOldSE = (SEProcPtr) GetTrapAddress(_SystemEvent);
  84.  
  85.     /* move the code into place after the globals */
  86.     BlockMove(StartPatchCode, patchPtr + sizeof(PatchGlobals), codeSize);
  87.  
  88.     /* move to the next routine */
  89.     patchPtr += sizeof(PatchGlobals);
  90.     offset = (long) MySystemEvent - (long) StartPatchCode;
  91.  
  92.     /* make a UniversalProcedurePointer for the SystemEvent patch */
  93.     pgPtr->pgMySystemEventUPP = NewRoutineDescriptor((ProcPtr) ((long) patchPtr + (long) offset),
  94.             kPascalStackBased, kM68kISA);
  95.  
  96.     /* set the SystemEvent patch */
  97.     SetToolTrapAddress(pgPtr->pgMySystemEventUPP, _SystemEvent);
  98.  
  99.  
  100.  
  101.     /* install a new 'sDmn' gestalt selector */
  102.     offset = (long) sDmnSelector - (long) StartPatchCode;
  103.     pgPtr->pgMysDmnSelectUPP = NewRoutineDescriptor((ProcPtr) ((long) patchPtr + (long) offset),
  104.             uppSelectorFunctionProcInfo, kM68kISA);
  105.     NewGestalt('sDmn', (SelectorFunctionUPP) pgPtr->pgMysDmnSelectUPP);
  106.  
  107.     /* install a new 'SAVR' gestalt selector */
  108.     offset = (long) SAVRSelector - (long) StartPatchCode;
  109.     pgPtr->pgMySAVRSelectUPP = NewRoutineDescriptor((ProcPtr) ((long) patchPtr + (long) offset),
  110.             uppSelectorFunctionProcInfo, kM68kISA);
  111.     NewGestalt('SAVR', (SelectorFunctionUPP) pgPtr->pgMySAVRSelectUPP);
  112.  
  113.     /* install a new 'SAVC' gestalt selector */
  114.     offset = (long) SAVCSelector - (long) StartPatchCode;
  115.     pgPtr->pgMySAVCSelectUPP = NewRoutineDescriptor((ProcPtr) ((long) patchPtr + (long) offset),
  116.             uppSelectorFunctionProcInfo, kM68kISA);
  117.     NewGestalt('SAVC', (SelectorFunctionUPP) pgPtr->pgMySAVCSelectUPP);
  118.  
  119.  
  120.  
  121.     pgPtr->pgSaverOn = false;
  122.     pgPtr->pgMustSleep = false;
  123.     pgPtr->pgMustWake = false;
  124.     pgPtr->pgInSleepRect = false;
  125.     pgPtr->pgLastAction = LMGetTicks();
  126.  
  127.     /* if we got this far, put up the appropriate icon */
  128.     if (usingPrefsFile == false) {
  129.         DisplayIcon(kPrefIcon);
  130.     } else {
  131.         DisplayIcon(pgPtr->pgMustSave == true ? kNormalIcon : kOffIcon);
  132.     }
  133. }
  134.  
  135.  
  136.  
  137. /*********************************************************************
  138.  * OpenPrefsFile:
  139.  *
  140.  * Open up our prefs file. If there are problems, then create a new
  141.  * prefs file. (Also in ScreenDaemon Control.c)
  142.  *
  143.  *********************************************************************/
  144. Boolean OpenPrefsFile()
  145. {
  146.     FSSpec    theSpec;
  147.     OSErr    fsErr;
  148.     long    result;
  149.     short    resAttrs, theResFile, thePrefsFile;
  150.     Handle    prefHandle, tmplHandle, stringHandle; /* FIXME - use just one handle! */
  151.     Str255    theString;
  152.     
  153.     /*     Check to see if FindFolder is available */
  154.     if ((Gestalt(gestaltFindFolderAttr, &result) != noErr) ||
  155.             !(result & Bit(gestaltFindFolderPresent)))
  156.         return false;
  157.     
  158.     /* See if we can find the preferences folder */
  159.     if (FindFolder(kOnSystemDisk, kPreferencesFolderType, kCreateFolder,
  160.             &(theSpec.vRefNum), &(theSpec.parID)) != noErr)
  161.         return false;
  162.     
  163.     /* Get the file name */
  164.     stringHandle = GetIndResource('STR ', 1);
  165.     if (ResError() != noErr)
  166.         return false;
  167.     BlockMoveData(*stringHandle, theString, 256);
  168.     ReleaseResource(stringHandle);
  169.  
  170.     /* Make a file specification */
  171.     switch (FSMakeFSSpec(theSpec.vRefNum, theSpec.parID, theString, &theSpec)) {
  172.     
  173.         /* We need to create the file */
  174.         case fnfErr:
  175.             /* make a new file */
  176.             FSpCreateResFile(&theSpec, 'sDmn', 'pref', smSystemScript);
  177.             if (ResError() != noErr)
  178.                 return false;
  179.             
  180.             /* Open it, and copy over our default resources */
  181.             theResFile = CurResFile();
  182.             thePrefsFile = FSpOpenResFile(&theSpec, fsRdWrPerm);
  183.             
  184.             /* If we can't open the freshly created file, punt */
  185.             if (ResError() != noErr)
  186.                 return false;
  187.             
  188.             /* We're open... copy away */
  189.             
  190.             /* Get preferences resource */
  191.             UseResFile(theResFile);
  192.             prefHandle = GetIndResource('PREF', 1);
  193.             resAttrs = GetResAttrs(prefHandle);
  194.             DetachResource(prefHandle);
  195.             
  196.             /* Get the name of our prefs resource */
  197.             stringHandle = GetIndResource('STR ', 2);
  198.             if (ResError() != noErr)
  199.                 return false;
  200.             BlockMoveData(*stringHandle, theString, 256);
  201.             ReleaseResource(stringHandle);
  202.  
  203.             /* Write preferences resource */
  204.             UseResFile(thePrefsFile);
  205.             AddResource(prefHandle, 'PREF', 128, theString);
  206.             SetResAttrs(prefHandle, resAttrs);
  207.             ChangedResource(prefHandle);
  208.             WriteResource(prefHandle);
  209.             ReleaseResource(prefHandle);
  210.  
  211.             /* Get template resource */
  212.             UseResFile(theResFile);
  213.             tmplHandle = GetIndResource('TMPL', 1);
  214.             resAttrs = GetResAttrs(tmplHandle);
  215.             DetachResource(tmplHandle);
  216.             
  217.             /* Get the name of our prefs resource */
  218.             stringHandle = GetIndResource('STR ', 3);
  219.             if (ResError() != noErr)
  220.                 return false;
  221.             BlockMoveData(*stringHandle, theString, 256);
  222.             ReleaseResource(stringHandle);
  223.  
  224.             /* Write template resource */
  225.             UseResFile(thePrefsFile);
  226.             AddResource(tmplHandle, 'TMPL', 128, theString);
  227.             SetResAttrs(tmplHandle, resAttrs);
  228.             ChangedResource(tmplHandle);
  229.             WriteResource(tmplHandle);
  230.             ReleaseResource(tmplHandle);
  231.             
  232.             /* All done */
  233.             return true;
  234.             break;
  235.         
  236.         /* Everything went fine - open the file */
  237.         case noErr:
  238.             FSpOpenResFile(&theSpec, fsRdPerm);
  239.             return true;
  240.             break;
  241.         
  242.         /* We don't handle any other errors */
  243.         default:
  244.             return false;
  245.             break;
  246.     }
  247.     
  248.     /* This will never execute, because we'll never get here */
  249.     return false;
  250. }
  251.  
  252.  
  253.  
  254. /*********************************************************************
  255.  * DisplayIcon:
  256.  *
  257.  * Put up the appropriate icon.
  258.  *
  259.  *********************************************************************/
  260. void DisplayIcon(short iconID)
  261. {
  262.     typedef pascal short (*ShowInitProcPtr) (short,short);
  263.  
  264.     Handle            procH;
  265.     ShowInitProcPtr    ShowInitProc;
  266.     
  267.     /* Load in the ShowInit PROC and call it */
  268.     if ((procH = GetIndResource('PROC', 1)) != 0L) {
  269.         HLock(procH);
  270.         ShowInitProc = (ShowInitProcPtr) *procH;
  271.         (*ShowInitProc) (iconID, -1);
  272.         HUnlock(procH);
  273.         ReleaseResource(procH);
  274.     }
  275. }
  276.  
  277.  
  278.  
  279. /*********************************************************************
  280.  * StartPatchCode:
  281.  *
  282.  * Dummy proc to mark the beginning of the code for the
  283.  * patches. Make sure all of your patch code is between
  284.  * here and EndPatchCode.
  285.  *
  286.  *********************************************************************/
  287. void StartPatchCode()
  288. {
  289. }
  290.  
  291.  
  292.  
  293. /*********************************************************************
  294.  * MySystemEvent:
  295.  *
  296.  * Tail patch on SystemEvent.
  297.  *
  298.  * If you want to eat the event (not pass it on to the caller) then set
  299.  * returnValue to true.
  300.  *
  301.  *********************************************************************/
  302. pascal short MySystemEvent(EventRecord *theEvent)
  303. {
  304.     PatchGlobalsPtr    pgPtr;
  305.     short            returnValue;
  306.     /* register short    foo; */
  307.     short            foo;    /* Was the register assignment causing us problems? */
  308.     
  309.     /* find our globals */
  310.     pgPtr = (PatchGlobalsPtr) ((long) StartPatchCode - sizeof(PatchGlobals));
  311.     
  312.     /* call original SE first */
  313.     returnValue = (*pgPtr->pgOldSE) (theEvent);
  314.     
  315.     if ((foo = theEvent->what) >= 1 && foo <= 5 || foo == 7) {
  316.         pgPtr->pgLastAction = LMGetTicks();    /* Update last time counter */
  317.         pgPtr->pgInSleepRect = false;
  318.         if (pgPtr->pgSaverOn) {
  319.             if (foo != 7) {                    /* Mask keypresses and clicks */
  320.                                             /* but allow disk mounts. (disk == 7) */
  321.                 returnValue = true;            /* We've "handled" the event. */
  322.                 theEvent->what = 0;
  323.             }
  324.             pgPtr->pgMustWake = true;
  325.         }
  326.     }
  327.     
  328.     /* return to original caller */
  329.     return returnValue;
  330. }
  331.  
  332.  
  333.  
  334. /*********************************************************************
  335.  * sDmnSelector:
  336.  *
  337.  * 'sDmn' gestalt selector that provides the address of our patch
  338.  * globals. This is *much* cleaner than patching SizeResource().
  339.  *
  340.  *********************************************************************/
  341. pascal void sDmnSelector(OSType selector, long *result)
  342. {
  343.     /* find our globals ...and give 'em back! */
  344.     *result = (long) ((long) StartPatchCode - sizeof(PatchGlobals));
  345. }
  346.  
  347.  
  348.  
  349. /*********************************************************************
  350.  * SAVRSelector:
  351.  *
  352.  * 'SAVR' gestalt selector that tells the world when we're alive and
  353.  * also when we're sleeping.
  354.  *
  355.  *********************************************************************/
  356. pascal void SAVRSelector(OSType selector, long *result)
  357. {
  358.     PatchGlobalsPtr    pgPtr;
  359.     
  360.     /* find our globals */
  361.     pgPtr = (PatchGlobalsPtr) ((long) StartPatchCode - sizeof(PatchGlobals));
  362.  
  363.     *result = 0;
  364.  
  365.     /* if we haven't been disabled, tell the world of our presence */
  366.     if (pgPtr->pgMustSave == true)
  367.         *result |= Bit(gestaltSaverTurnedOn);    // saver enabled
  368.  
  369.     /* If screen is darkened, tell the world we're alive */
  370.     if (pgPtr->pgSaverOn == true) {
  371.         *result |= Bit(gestaltSaverAsleep);        // saver asleep
  372.     }
  373. }
  374.  
  375.  
  376.  
  377. /*********************************************************************
  378.  * SAVCSelector:
  379.  *
  380.  * The 'SAVC' gestalt selector passes back the address of our control
  381.  * routine so that other applications can control us.
  382.  *
  383.  * We can be turned on and off remotely, and we can be told to put
  384.  * the screen to sleep and wake the screen up. Neat, huh?
  385.  *
  386.  *********************************************************************/
  387. pascal void SAVCSelector(OSType selector, long *result)
  388. {
  389.     *result = (long) SaverControl;
  390. }
  391.  
  392.  
  393.  
  394. /*********************************************************************
  395.  * SaverControl:
  396.  *
  397.  * Executes a command from an application or whatever.
  398.  *
  399.  *********************************************************************/
  400. pascal OSErr SaverControl(short theCommand)
  401. {
  402.     PatchGlobalsPtr    pgPtr;
  403.     
  404.     /* find our globals */
  405.     pgPtr = (PatchGlobalsPtr) ((long) StartPatchCode - sizeof(PatchGlobals));
  406.  
  407.     switch (theCommand) {
  408.         case eSaverWakeUp:                    // Make ScreenDaemon wake up.
  409.             if (pgPtr->pgSaverOn)
  410.                 pgPtr->pgMustWake = true;
  411.             break;
  412.             
  413.         case eSaverSleep:                    // Make ScreenDaemon go to sleep.
  414.             if (pgPtr->pgMustSave)
  415.                 pgPtr->pgMustSleep = true;
  416.             break;
  417.             
  418.         case eSaverOn:                        // Turn ScreenDaemon on.
  419.             pgPtr->pgMustSave = true;
  420.             pgPtr->pgMustSleep = false;
  421.             break;
  422.             
  423.         case eSaverOff:                        // Turn ScreenDaemon off.
  424.             if (pgPtr->pgSaverOn)
  425.                 pgPtr->pgMustWake = true;
  426.             pgPtr->pgMustSave = false;
  427.             break;
  428.     }
  429.     
  430.     return noErr;
  431. }
  432.  
  433.  
  434.  
  435. /*********************************************************************
  436.  * EndPatchCode:
  437.  *
  438.  * Dummy proc to mark the end of the code for the patches.
  439.  * Make sure all of your patch code is between here and
  440.  * StartPatchCode.
  441.  *
  442.  *********************************************************************/
  443. void EndPatchCode()
  444. {
  445. }
  446.  
  447.  
  448.  
  449. /*********************************************************************
  450.  * IsGammaAvailable:
  451.  *
  452.  * Tests availability of gamma fading support.
  453.  *
  454.  *********************************************************************/
  455. pascal Boolean IsGammaAvailable()
  456. {
  457.     GDHandle theGDevice;
  458.  
  459.     if (NGetTrapAddress(kGetDeviceListTrapNum, ToolTrap) ==
  460.             NGetTrapAddress(_Unimplemented, ToolTrap)) return(FALSE);
  461.     
  462.     for(theGDevice = GetDeviceList(); theGDevice; theGDevice = GetNextDevice(theGDevice)) {
  463.         if (TestDeviceAttribute(theGDevice, screenDevice) && 
  464.                 TestDeviceAttribute(theGDevice, noDriver)) return(FALSE);
  465.         if ((*theGDevice)->gdType == fixedType) return(FALSE);
  466.     }
  467.         
  468.     return(TRUE);
  469. }